package com.jwgf.smarowindingmachine_unity.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import androidx.fragment.app.DialogFragment;

import com.jwgf.adapter.CommonAdapter;
import com.jwgf.adapter.ViewHolder;
import com.jwgf.protocolstack.CommunicationStateListener;
import com.jwgf.protocolstack.ProtocolStack;
import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.smarowindingmachine_unity.logic.CanCommand;
import com.jwgf.smarowindingmachine_unity.logic.MachineParameterSharedPreference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TensionSampleDialog extends DialogFragment implements CommunicationStateListener.ReadSucceedListener {
    public static final String GROUP = "group";
    private static final String SPINDLE = "spindle";
    private static final String ORIGIN = "origin";
    private static final String BASE = "base";

    private ListView list;
    List<Map<String, String>> listData = new ArrayList<Map<String, String>>();
    CommonAdapter<Map<String, String>> adapter;
    private MachineParameterSharedPreference sp = MachineParameterSharedPreference.getInstance();

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Bundle bd = getArguments();
        int group = bd.getInt(GROUP);
        SmaroWindingMachineActivity.canHandStack.registerReadSucceedListener(this);
        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.getBus());
        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.createGroupCmdFunction(group, 0x40, 0x2D));
        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.createGroupCmdFunction(group, 0x40, 0x35));
//        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(sp.createCanCmd(group, 0x40, 0x00, String.valueOf(Integer.parseInt("2D", 16))));
//        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(sp.createCanCmd(group, 0x40, 0x00, String.valueOf(Integer.parseInt("35", 16))));
        SmaroWindingMachineActivity.canHandStack.putPriorityCmd(CanCommand.releaseBus());
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View v = inflater.inflate(R.layout.list_view, null);
        list = (ListView) v.findViewById(R.id.list);

        HashMap<String, String> map = new HashMap<String, String>();

        map.put(SPINDLE, "单锭号");
        map.put(ORIGIN, "采样原始值");
        map.put(BASE, "采样基准值");
        listData.add(map);
//		byte[] data = new byte[10];
//		data[2] = 0x2d;
//		data[3] = 2;
//		data[4] = 2;
//		data[5] = (byte) 0x95;
        adapter = new CommonAdapter<Map<String, String>>(
                getActivity(), listData, R.layout.item_tension_sample_list) {

            @Override
            public void convert(ViewHolder holder, final Map<String, String> item) {
                holder.setText(R.id.spindle, item.get(SPINDLE));
                holder.setText(R.id.origin, item.get(ORIGIN));
                holder.setText(R.id.base, item.get(BASE));
            }
        };
        list.setAdapter(adapter);
//		readSucceed(null, null, data);

        builder.setTitle(R.string.tension_sample_value);
        builder.setView(v);
        return builder.create();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        SmaroWindingMachineActivity.canHandStack.unregisterReadSucceedListener(this);
    }

    @Override
    public void readSucceed(ProtocolStack ps, byte[] request, byte[] response) {
        int canfilter = response[2];
        int spinle = response[3] - 1;
        int origin = ((response[5] & 0xff) << 8) | (response[4] & 0xff);
        int base = ((response[7] & 0xff) << 8) | (response[6] & 0xff);
        if (canfilter == 0x2d) {
            final HashMap<String, String> map = new HashMap<String, String>();
            map.put(SPINDLE, String.valueOf(spinle));
            map.put(ORIGIN, String.valueOf(origin));
            map.put(BASE, String.valueOf(base));
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    listData.add(map);
                    adapter.notifyDataSetChanged();
                }
            });

        }
    }
}